// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"server/components/models/ent/region"
	"server/components/models/ent/regiontype"
	"server/components/models/ent/schema"
	"server/components/models/ent/user"
	"strings"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
)

// Region is the model entity for the Region schema.
type Region struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// Name holds the value of the "name" field.
	Name string `json:"name,omitempty"`
	// ParentRegion holds the value of the "parentRegion" field.
	ParentRegion string `json:"parentRegion,omitempty"`
	// Latitude1 holds the value of the "latitude1" field.
	Latitude1 float64 `json:"latitude1,omitempty"`
	// Longitude1 holds the value of the "longitude1" field.
	Longitude1 float64 `json:"longitude1,omitempty"`
	// Latitude2 holds the value of the "latitude2" field.
	Latitude2 float64 `json:"latitude2,omitempty"`
	// Longitude2 holds the value of the "longitude2" field.
	Longitude2 float64 `json:"longitude2,omitempty"`
	// OsmData holds the value of the "osmData" field.
	OsmData schema.OsmData `json:"osmData,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the RegionQuery when eager-loading is set.
	Edges              RegionEdges `json:"edges"`
	region_region_type *int64
	region_account_id  *int
	selectValues       sql.SelectValues
}

// RegionEdges holds the relations/edges for other nodes in the graph.
type RegionEdges struct {
	// RegionType holds the value of the regionType edge.
	RegionType *RegionType `json:"regionType,omitempty"`
	// AccountId holds the value of the accountId edge.
	AccountId *User `json:"accountId,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [2]bool
}

// RegionTypeOrErr returns the RegionType value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e RegionEdges) RegionTypeOrErr() (*RegionType, error) {
	if e.RegionType != nil {
		return e.RegionType, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: regiontype.Label}
	}
	return nil, &NotLoadedError{edge: "regionType"}
}

// AccountIdOrErr returns the AccountId value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e RegionEdges) AccountIdOrErr() (*User, error) {
	if e.AccountId != nil {
		return e.AccountId, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: user.Label}
	}
	return nil, &NotLoadedError{edge: "accountId"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Region) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case region.FieldOsmData:
			values[i] = new([]byte)
		case region.FieldLatitude1, region.FieldLongitude1, region.FieldLatitude2, region.FieldLongitude2:
			values[i] = new(sql.NullFloat64)
		case region.FieldID:
			values[i] = new(sql.NullInt64)
		case region.FieldName, region.FieldParentRegion:
			values[i] = new(sql.NullString)
		case region.ForeignKeys[0]: // region_region_type
			values[i] = new(sql.NullInt64)
		case region.ForeignKeys[1]: // region_account_id
			values[i] = new(sql.NullInt64)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Region fields.
func (r *Region) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case region.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			r.ID = int64(value.Int64)
		case region.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				r.Name = value.String
			}
		case region.FieldParentRegion:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field parentRegion", values[i])
			} else if value.Valid {
				r.ParentRegion = value.String
			}
		case region.FieldLatitude1:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field latitude1", values[i])
			} else if value.Valid {
				r.Latitude1 = value.Float64
			}
		case region.FieldLongitude1:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field longitude1", values[i])
			} else if value.Valid {
				r.Longitude1 = value.Float64
			}
		case region.FieldLatitude2:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field latitude2", values[i])
			} else if value.Valid {
				r.Latitude2 = value.Float64
			}
		case region.FieldLongitude2:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field longitude2", values[i])
			} else if value.Valid {
				r.Longitude2 = value.Float64
			}
		case region.FieldOsmData:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field osmData", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &r.OsmData); err != nil {
					return fmt.Errorf("unmarshal field osmData: %w", err)
				}
			}
		case region.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field region_region_type", value)
			} else if value.Valid {
				r.region_region_type = new(int64)
				*r.region_region_type = int64(value.Int64)
			}
		case region.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field region_account_id", value)
			} else if value.Valid {
				r.region_account_id = new(int)
				*r.region_account_id = int(value.Int64)
			}
		default:
			r.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the Region.
// This includes values selected through modifiers, order, etc.
func (r *Region) Value(name string) (ent.Value, error) {
	return r.selectValues.Get(name)
}

// QueryRegionType queries the "regionType" edge of the Region entity.
func (r *Region) QueryRegionType() *RegionTypeQuery {
	return NewRegionClient(r.config).QueryRegionType(r)
}

// QueryAccountId queries the "accountId" edge of the Region entity.
func (r *Region) QueryAccountId() *UserQuery {
	return NewRegionClient(r.config).QueryAccountId(r)
}

// Update returns a builder for updating this Region.
// Note that you need to call Region.Unwrap() before calling this method if this Region
// was returned from a transaction, and the transaction was committed or rolled back.
func (r *Region) Update() *RegionUpdateOne {
	return NewRegionClient(r.config).UpdateOne(r)
}

// Unwrap unwraps the Region entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (r *Region) Unwrap() *Region {
	_tx, ok := r.config.driver.(*txDriver)
	if !ok {
		panic("ent: Region is not a transactional entity")
	}
	r.config.driver = _tx.drv
	return r
}

// String implements the fmt.Stringer.
func (r *Region) String() string {
	var builder strings.Builder
	builder.WriteString("Region(")
	builder.WriteString(fmt.Sprintf("id=%v, ", r.ID))
	builder.WriteString("name=")
	builder.WriteString(r.Name)
	builder.WriteString(", ")
	builder.WriteString("parentRegion=")
	builder.WriteString(r.ParentRegion)
	builder.WriteString(", ")
	builder.WriteString("latitude1=")
	builder.WriteString(fmt.Sprintf("%v", r.Latitude1))
	builder.WriteString(", ")
	builder.WriteString("longitude1=")
	builder.WriteString(fmt.Sprintf("%v", r.Longitude1))
	builder.WriteString(", ")
	builder.WriteString("latitude2=")
	builder.WriteString(fmt.Sprintf("%v", r.Latitude2))
	builder.WriteString(", ")
	builder.WriteString("longitude2=")
	builder.WriteString(fmt.Sprintf("%v", r.Longitude2))
	builder.WriteString(", ")
	builder.WriteString("osmData=")
	builder.WriteString(fmt.Sprintf("%v", r.OsmData))
	builder.WriteByte(')')
	return builder.String()
}

// Regions is a parsable slice of Region.
type Regions []*Region
